<?php
namespace application\datatypes;
use application\utilities\Utility;

class GenericList extends DataType {
	public static $name = 'Generic List';
	public static $fields = array();
	
	public static $displayParams = array(
		array(
			'label'=>'Format',
			'name'=>'format',
			'type'=>'select',
			'defaultValue'=>'ul',
			'options'=>array(
				array(
					'label'=>'Unordered List',
					'value'=>'ul'
				),
				array(
					'label'=>'Ordered List',
					'value'=>'ol'
				),
				array(
					'label'=>'Comma-Separated Values',
					'value'=>'csv'
				)
			)
		)
	);
	
	public static function formHeader($field) {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript(getCommonFile('concerto/javascript/generic-list.js'));
		
	}
	
	public static function validate($field, $data, $results) {
		if($field->cf_required && !$data['list_'.$field->cf_id]) {
			$results['errors'][] = $field->cf_display_name.' is required.';
		}
		else {
			$results['db_fields']['list'][$field->cf_id] = $data['list_'.$field->cf_id];
		}
		
		return $results;
	}
	
	public static function display($formElements) {
		?>
		
		<fieldset class="list-field">
			<header><?=$formElements['display']?></header>
			<section class="inner">
			<?php if($description = $formElements['description']) : ?>
				<p class="form-description"><?=$description?></p>
			<?php endif;?>
			<?=$formElements['elements']['element']?>
			</section>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		

		
		/** Display tags */
		$formElements = array('column'=>'tags', 'display'=>$field->cf_display_name, 'data_type'=>'Tags', 'elements'=>array());
	
		
		$existingItems = $data['list_'.$field->cf_id];
		if(!$existingItems) $existingItems = array();
		
		$element = '<ul class="list-field" id="'.$field->cf_column.'">';
		foreach($existingItems as $item) {
			if(is_numeric($item)) { 
				$value = DB()->getVar("SELECT `cfv_text1` FROM `cn_content_field_values` WHERE `cfv_id`='$item'");
			}
			else {
				$value = $item;
			}
			$element .= '<li><input class="list-item-text" type="text" name="list_'.$field->cf_id.'[]" value="'.$value.'"/><button class="form-button remove-list-item">X</button></li>';
		}
		$element .= '<li class="add-list-item-li"><button data-field-id="'.$field->cf_id.'" class="form-button add-list-item">Add Item</button></li>';
		$element .= '</ul>';
		$formElements['elements']['element'] = $element;

		
		return $formElements;
	
	}
	
	public static function endForm($field, $data, $id, $type) {
		$listItems = $data['list'][$field->cf_id];
		if(!$listItems) $listItems = array();
		foreach($listItems as $key=>$item) {
			if(!$item) {
				unset($listItems[$key]);			
			}
		}
		
		
		
		$existing = DB()->getVarArray("SELECT `cfv_id` FROM `cn_content_field_values` WHERE `cfv_field`='$field->cf_id' AND `cfv_content`='$id'");
		
		$formCount = count($listItems);
		
		foreach($listItems as $key=>$item) {
			if($key < count($existing)) {
				DB()->update('cn_content_field_values', 'cfv_id', $existing[$key], array('cfv_text1'=>$item));
				unset($existing[$key]);
				
			}
			else {
				DB()->insert('cn_content_field_values', array('cfv_text1'=>$item, 'cfv_content'=>$id, 'cfv_field'=>$field->cf_id));
			}
		}
		
		foreach($existing as $delete) {
			DB()->query("DELETE FROM `cn_content_field_values` WHERE `cfv_id`='$delete'");
		}
		
	}
	
	public static function fromDB($fields, $field, $data) {
		$column = $field->cf_column;
		$contentId = $data['content_id'];
		$existingItems = DB()->getVarArray("SELECT `cfv_id` FROM `cn_content_field_values` WHERE `cfv_field`='$field->cf_id' AND `cfv_content`='$contentId'");
	

		return array('list_'.$field->cf_id => $existingItems);
	}
	
	
	
	public static function prepareData($field, $data, $params) {
		$items = $data['list_'.$field->cf_id];
		$format = $params['format'];
		foreach($items as $key=>$item) {
			$items[$key] = DB()->getVar("SELECT `cfv_text1` FROM `cn_content_field_values` WHERE `cfv_id`='$item'");
		}
		if(!count($items)) {
			return 'None';
		}
				
		switch($format) {
			case 'ul':
			default:
				$return = '<ul>';
				foreach($items as $item) {
					$return .= '<li>'.$item.'</li>';
				}
				$return .= '</ul>';
				break;
			case 'ol':
				$return = '<ol>';
				foreach($items as $item) {
					$return .= '<li>'.$item.'</li>';
				}
				$return .= '</ol>';
				break;
			case 'csv':
				$return = implode(', ', $items);
				break;
			case 'array':
				$return = $items;
				break;
		}
		
		return $return;
	}
	
}
?>